package com.rybbaby.tss.front.tssMemberCheckAttendance.controller;

import com.rybbaby.tss.core.annotation.RequestLog;
import com.rybbaby.tss.core.annotation.Token;
import com.rybbaby.tss.core.annotation.validator.RegexpValidator;
import com.rybbaby.tss.core.annotation.validator.StringValidator;
import com.rybbaby.tss.core.bean.PageBean;
import com.rybbaby.tss.core.consts.RoleConsts;
import com.rybbaby.tss.core.hqlsearch.HqlGenerateUtil;
import com.rybbaby.tss.core.utils.AjaxJsonResponse;
import com.rybbaby.tss.core.utils.PropertiesReadUtil;
import com.rybbaby.tss.core.utils.SequenceUtil;
import com.rybbaby.tss.core.utils.StringUtils;
import com.rybbaby.tss.core.web.controller.BaseController;
import com.rybbaby.tss.front.tssClasses.service.ITssClassesService;
import com.rybbaby.tss.front.tssMealFee.entity.TssMealFee;
import com.rybbaby.tss.front.tssMealFee.service.ITssMealFeeService;
import com.rybbaby.tss.front.tssMemberAttendClassRecord.service.ITssMemberAttendClassRecordService;
import com.rybbaby.tss.front.tssMemberCheckAttendance.entity.TssMemberCheckAttendance;
import com.rybbaby.tss.front.tssMemberCheckAttendance.entity.ViewAttendanceDetails;
import com.rybbaby.tss.front.tssMemberCheckAttendance.entity.ViewAttendanceMaintenance;
import com.rybbaby.tss.front.tssMemberCheckAttendance.entity.ViewMemberAttendClassRecord;
import com.rybbaby.tss.front.tssMemberCheckAttendance.service.ITssMemberCheckAttendanceService;
import com.rybbaby.tss.front.tssMemberCheckAttendance.service.IViewMemberAttendClassRecordService;
import com.rybbaby.tss.front.tssMemberCheckAttendanceHistory.service.ITssMemberCheckAttendanceHistoryService;
import com.rybbaby.tss.front.tssMemberPackage.entity.TssMemberPackage;
import com.rybbaby.tss.front.tssMemberPackage.service.ITssMemberPackageService;
import com.rybbaby.tss.front.tssMemberPackageHistory.service.ITssMemberPackageHistoryService;
import com.rybbaby.tss.front.tssMemberReturnPremium.service.ITssMemberReturnPremiumService;
import com.rybbaby.tss.front.tssPayment.service.ITssPaymentService;
import com.rybbaby.tss.front.tssRightStatistic.entity.TssRightStatistic;
import com.rybbaby.tss.front.tssRightStatistic.service.ITssRightStatisticService;
import com.rybbaby.tss.front.tssStudent.service.ITssStudentService;
import com.rybbaby.tss.platform.sysAdminUser.entity.SysAdminUser;
import com.rybbaby.tss.platform.sysAdminUser.service.ISysAdminUserService;
import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Order;
import org.hibernate.criterion.Restrictions;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import java.io.IOException;
import java.io.Serializable;
import java.text.SimpleDateFormat;
import java.util.*;

/**
 * @description 会员考勤记录表管理
 * 
 * @author 雷春琼
 * 
 * @createDate 2017-08-10 上午10:33:46
 */
@Controller
@RequestMapping(value = "tssMemberCheckAttendanceController")
public class TssMemberCheckAttendanceController extends BaseController {

	@Autowired
	private ITssMemberCheckAttendanceService tssMemberCheckAttendanceService;
	@Autowired
	private ITssMemberCheckAttendanceHistoryService tssMemberCheckAttendanceHistoryService;
	@Autowired
	private ITssMemberAttendClassRecordService tssMemberAttendClassRecordService;
	@Autowired
	private ITssMemberPackageService memberPackageService;
	@Autowired
	private ITssClassesService classesService;
	@Autowired
	private ITssStudentService tssStudentService;
	@Autowired
	private ITssPaymentService tssPaymentService;
	@Autowired
	private ITssMemberReturnPremiumService tssMemberReturnPremiumService;
	@Autowired
	private ITssMemberPackageService tssMemberPackageService;
	@Autowired
	private ITssMealFeeService tssMealFeeService;
	@Autowired
	private ITssRightStatisticService tssRightStatisticService;
	@Autowired
	private ITssMemberPackageHistoryService tssMemberPackageHistoryService;
	@Autowired
	private ISysAdminUserService sysAdminUserService;
	@Autowired
	private IViewMemberAttendClassRecordService viewMemberAttendClassRecordService;
	/**
	 * 获取会员考勤记录表列表
	 * 
	 * @return
	 * @throws IOException
	 */
	@RequestLog(content = "获取会员考勤记录表列表" , save = true)
	@Token(validateToken = false)
	@RequestMapping(value = "tssMemberCheckAttendanceList" , method = RequestMethod.POST)
	public ModelAndView tssMemberCheckAttendanceList(TssMemberCheckAttendance entity , PageBean paramPageBean) {
		ModelAndView mav = new ModelAndView(PropertiesReadUtil.getStringProperty("tssMemberCheckAttendanceListView"));
		try {
			//获取当前登录者
			String adminUserId = super.getLoginUser().getId();
			SysAdminUser user = super.getLoginUser();
			String ids = "";
			SysAdminUser sysAdminUser=this.sysAdminUserService.get(adminUserId);
			//获取当前登录者所拥有角色
			List<String> roleList = super.getLoginAdminRoles();

			String adminUserIds = "";
			if(roleList.contains(RoleConsts.TSS_COUNSELOR)||roleList.contains(RoleConsts.TSS_TEACHER)){
				//dc.add(Restrictions.sqlRestriction(" adminUserId in ('" + adminUserId + "') "));
				adminUserIds = adminUserId;

			} else{
				List<String> idsList = super.getUserIdByRoleAndSchololId(sysAdminUser.getSchoolId());
				ids = StringUtils.join(idsList, "','");
				//dc.add(Restrictions.sqlRestriction(" adminUserId in ('" + ids + "') "));
				adminUserIds = ids;
			}

			Map<String , String []> unLockMap = getParameterMap(request);
			DetachedCriteria dc = DetachedCriteria.forClass(TssMemberCheckAttendance.class);
			HqlGenerateUtil.fillDetachedCriteria(dc , entity , unLockMap);
			PageBean pageBean = tssMemberCheckAttendanceService.pageList(dc , paramPageBean.getPageNo() , paramPageBean.getPageSize());

			//今日数据
			//新增/续报,亲子会员/过渡会员
			List<Map<String , Object>> todayStudentMap = tssStudentService.findTodayStudentByType(adminUserIds);
			//收付收入
			Map<String , Object> todayPaymentMap = tssPaymentService.findTodayPayment(adminUserIds);
			//退费支出
			Map<String , Object> todayRefundMap = tssMemberReturnPremiumService.findTodayRefund(adminUserIds);
			//权责收入
			Map<String , Object> todayResponsibilityMap = tssRightStatisticService.findTodayResponsibility(adminUserIds);

			//本月数据
			//新增/续报,亲子会员/过渡会员
			List<Map<String , Object>> monthStudentMap = tssStudentService.findMonthStudentByType(adminUserIds);
			//收付收入
			Map<String , Object> monthPaymentMap = tssPaymentService.findMonthPayment(adminUserIds);
			//退费支出
			Map<String , Object> monthRefundMap = tssMemberReturnPremiumService.findMonthRefund(adminUserIds);
			//权责收入
			Map<String , Object> monthResponsibilityMap = tssRightStatisticService.findMonthResponsibility(adminUserIds);

			//今日亲子/过渡考勤 统计
			List<Map<String , Object>> todayAttendanceStatisticsMap = tssMemberCheckAttendanceService.findTodayAttendanceStatisticsByType(adminUserIds);
			for (Map map : todayAttendanceStatisticsMap) {
				Integer sumRealAttendance =  Integer.parseInt(map.get("sumRealAttendance") == null ? "" : map.get("sumRealAttendance").toString());
				Integer sumShouldAttendAttendance =  Integer.parseInt(map.get("sumShouldAttendAttendance") == null ? "" : map.get("sumShouldAttendAttendance").toString());
				int ratio = 0;
				if(sumShouldAttendAttendance != 0){
					ratio = sumRealAttendance*100/sumShouldAttendAttendance;
				}
				map.put("ratio",ratio);
			}
			//今日亲子/过渡考勤 班级列表
			List<Map<String , Object>> ClassAttendanceMap = tssMemberCheckAttendanceService.findClassAttendanceByType(adminUserIds);
			List<Map<String , Object>> parentingClassAttendanceMap = new ArrayList<Map<String , Object>>();
			List<Map<String , Object>> transitionClassAttendanceMap = new ArrayList<Map<String , Object>>();
			for (Map map : ClassAttendanceMap) {
				Integer sumRealAttendance =  Integer.parseInt(map.get("sumRealAttendance") == null ? "" : map.get("sumRealAttendance").toString());
				Integer sumShouldAttendAttendance =  Integer.parseInt(map.get("sumShouldAttendAttendance") == null ? "" : map.get("sumShouldAttendAttendance").toString());
				int ratio = 0;
				if(sumShouldAttendAttendance != 0) {
					ratio = sumRealAttendance * 100 / sumShouldAttendAttendance;
				}
				map.put("ratio",ratio);
				if(map.get("type").toString().equals("1")){
					parentingClassAttendanceMap.add(map);
				}
				if(map.get("type").toString().equals("2")){
					transitionClassAttendanceMap.add(map);
				}


			}

			mav.addObject("todayStudentMap" , todayStudentMap);
			mav.addObject("todayPaymentMap" , todayPaymentMap);
			mav.addObject("todayRefundMap" , todayRefundMap);
			mav.addObject("todayResponsibilityMap" , todayResponsibilityMap);
			mav.addObject("monthStudentMap" , monthStudentMap);
			mav.addObject("monthPaymentMap" , monthPaymentMap);
			mav.addObject("monthRefundMap" , monthRefundMap);
			mav.addObject("monthResponsibilityMap" , monthResponsibilityMap);
			mav.addObject("todayAttendanceStatisticsMap" , todayAttendanceStatisticsMap);
			//mav.addObject("ClassAttendanceMap" , ClassAttendanceMap);
			mav.addObject("parentingClassAttendanceMap" , parentingClassAttendanceMap);
			mav.addObject("transitionClassAttendanceMap" , transitionClassAttendanceMap);


			mav.addObject(super.SEARCH_MAP , unLockMap);
			mav.addObject(super.PAGE_BEAN , pageBean);
			mav.addObject("searchFormId" , SEARCH_FORM);
			mav.addObject("actionUrl" , request.getRequestURI());
			mav.addObject("pagingBar" , pageBean.getPagingBar(SEARCH_FORM));
			mav.addObject("serializeNum" , SequenceUtil.getUpperCaseUUID());
			String requestURI = StringUtils.substringBeforeLast(request.getRequestURI() , "/");
			mav.addObject("editUrl" , requestURI + "/toEditView.do");
			mav.addObject("deleteUrl" , requestURI + "/delete.do");
			mav.addObject("batchDeleteUrl" , requestURI + "/batchDelete.do");
		} catch (Exception e) {
			throw e;
		}
		return mav;
	}
	/**
	 * 获取班级考勤相关信息
	 *
	 * @return
	 * @throws IOException
	 */
	@RequestLog(content = "获取班级考勤相关信息" , save = true)
	@Token(validateToken = false)
	@RequestMapping(value = "tssMemberCheckAttendanceClass" , method = RequestMethod.GET)
	public ModelAndView tssMemberCheckAttendanceClass(ViewMemberAttendClassRecord entity , PageBean paramPageBean
			,@RequestParam(required = false) String type) throws Exception {
 		ModelAndView mav=new ModelAndView(PropertiesReadUtil.getStringProperty("tssMemberCheckAttendanceClassView"));;
		if(type!=null && !"".equals(type)){
			if("1".equals(type)){
				mav = new ModelAndView(PropertiesReadUtil.getStringProperty("tssMemberCheckAttendanceClassView"));
			}else{
				mav = new ModelAndView(PropertiesReadUtil.getStringProperty("tssMemberTransitionCheckAttendanceClassView"));
			}
		}
		try {
			Map<String , String []> unLockMap = getParameterMap(request);
			DetachedCriteria dc = DetachedCriteria.forClass(ViewMemberAttendClassRecord.class);
			HqlGenerateUtil.fillDetachedCriteria(dc , entity , unLockMap);
			PageBean pageBean = tssMemberCheckAttendanceService.pageList(dc , paramPageBean.getPageNo() , paramPageBean.getPageSize());
			List<ViewMemberAttendClassRecord> byDetached = viewMemberAttendClassRecordService.findByDetached(dc);
			List<TssMemberCheckAttendance> list = tssMemberCheckAttendanceService.attendanceNumber(entity.getLessonsId(),1);
			int ratio = 0;
			if(byDetached.size()>0 && list.size()>0){
				ratio = list.size()*100/byDetached.size();
				if(list.size() > 0){
					mav.addObject("type" , list.get(0).getType());
				}
			}

			mav.addObject("byDetached" , byDetached);
			mav.addObject("sumAttendance" , list.size());
			mav.addObject("sumPeople" , byDetached.size());
			mav.addObject("ratio" , ratio);
			mav.addObject(super.SEARCH_MAP , unLockMap);
			mav.addObject(super.PAGE_BEAN , pageBean);
			mav.addObject("searchFormId" , SEARCH_FORM);
			mav.addObject("actionUrl" , request.getRequestURI());
			mav.addObject("pagingBar" , pageBean.getPagingBar(SEARCH_FORM));
			mav.addObject("serializeNum" , SequenceUtil.getUpperCaseUUID());
			String requestURI = StringUtils.substringBeforeLast(request.getRequestURI() , "/");
			mav.addObject("editUrl" , requestURI + "/toEditView.do");
			mav.addObject("deleteUrl" , requestURI + "/delete.do");
			mav.addObject("batchDeleteUrl" , requestURI + "/batchDelete.do");
		} catch (Exception e) {
			throw e;
		}
		return mav;
	}
	/**
	 * 获取会员考勤记录表列表
	 *
	 * @return
	 * @throws IOException
	 */
	@RequestLog(content = "获取会员考勤记录表列表" , save = true)
	@Token(validateToken = false)
	@RequestMapping(value = "ViewAttendanceDetailsList" , method = RequestMethod.POST)
	public ModelAndView ViewAttendanceDetailsList(ViewAttendanceDetails entity , PageBean paramPageBean) {
		ModelAndView mav = new ModelAndView(PropertiesReadUtil.getStringProperty("ViewAttendanceDetailsListView"));
		try {
			Map<String , String []> unLockMap = getParameterMap(request);
			DetachedCriteria dc = DetachedCriteria.forClass(ViewAttendanceDetails.class);
			HqlGenerateUtil.fillDetachedCriteria(dc , entity , unLockMap);
			PageBean pageBean = tssMemberCheckAttendanceService.pageList(dc , paramPageBean.getPageNo() , paramPageBean.getPageSize());
			mav.addObject(super.SEARCH_MAP , unLockMap);
			mav.addObject(super.PAGE_BEAN , pageBean);
			mav.addObject("searchFormId" , SEARCH_FORM);
			mav.addObject("actionUrl" , request.getRequestURI());
			mav.addObject("pagingBar" , pageBean.getPagingBar(SEARCH_FORM));
			mav.addObject("serializeNum" , SequenceUtil.getUpperCaseUUID());
			String requestURI = StringUtils.substringBeforeLast(request.getRequestURI() , "/");
			mav.addObject("editUrl" , requestURI + "/toEditView.do");
			mav.addObject("deleteUrl" , requestURI + "/delete.do");
			mav.addObject("batchDeleteUrl" , requestURI + "/batchDelete.do");
		} catch (Exception e) {
			throw e;
		}
		return mav;
	}
	/**
	 * 获取考勤维护列表
	 *
	 * @return
	 * @throws IOException
	 */
	@RequestLog(content = "获取考勤维护列表" , save = true)
	@Token(validateToken = false)
	@RequestMapping(value = "viewAttendanceMaintenanceList" , method = RequestMethod.POST)
	public ModelAndView viewAttendanceMaintenanceList(ViewAttendanceMaintenance entity , PageBean paramPageBean) {
		ModelAndView mav = new ModelAndView(PropertiesReadUtil.getStringProperty("ViewAttendanceMaintenanceListView"));
		try {
			Map<String , String []> unLockMap = getParameterMap(request);
			String createTime_end = getParameter("createTime_end");
			SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
			if(createTime_end!=null && !createTime_end.equals("")){

			}else {
				Calendar lastDate = Calendar.getInstance();
				lastDate.add(Calendar.DAY_OF_YEAR, 0);
				String tomorrow = sdf.format(lastDate.getTime());
				unLockMap.put("createTime_end",new String[]{tomorrow});
			}
			DetachedCriteria dc = DetachedCriteria.forClass(ViewAttendanceMaintenance.class);
			HqlGenerateUtil.fillDetachedCriteria(dc , entity , unLockMap);
			String adminUserId = super.getLoginUser().getId();
			String ids = "";
			SysAdminUser sysAdminUser=this.sysAdminUserService.get(adminUserId);
			//获取当前登录者所拥有角色
			List<String> roleList = super.getLoginAdminRoles();

            if(roleList.contains(RoleConsts.TSS_COUNSELOR)||roleList.contains(RoleConsts.TSS_TEACHER)){
                dc.add(Restrictions.sqlRestriction(" adminUserId in ('" + adminUserId + "') "));
            } else{
                List<String> idsList = super.getUserIdByRoleAndSchololId(sysAdminUser.getSchoolId());
                ids = StringUtils.join(idsList, "','");
                dc.add(Restrictions.sqlRestriction(" adminUserId in ('" + ids + "') "));
            }
			dc.add(Restrictions.eq("type",1));
			dc.addOrder(Order.desc("createTime"));
			PageBean pageBean = tssMemberCheckAttendanceService.pageList(dc , paramPageBean.getPageNo() , paramPageBean.getPageSize());
			mav.addObject(super.SEARCH_MAP , unLockMap);
			mav.addObject(super.PAGE_BEAN , pageBean);
			mav.addObject("searchFormId" , SEARCH_FORM);
			mav.addObject("actionUrl" , request.getRequestURI());
			mav.addObject("pagingBar" , pageBean.getPagingBar(SEARCH_FORM));
			mav.addObject("serializeNum" , SequenceUtil.getUpperCaseUUID());
			String requestURI = StringUtils.substringBeforeLast(request.getRequestURI() , "/");
			mav.addObject("editUrl" , requestURI + "/toEditView.do");
			mav.addObject("deleteUrl" , requestURI + "/delete.do");
			mav.addObject("batchDeleteUrl" , requestURI + "/batchDelete.do");
		} catch (Exception e) {
			throw e;
		}
		return mav;
	}
	/**
	 * 获取过渡考勤维护列表
	 *
	 * @return
	 * @throws IOException
	 */
	@RequestLog(content = "获取过渡考勤维护列表" , save = true)
	@Token(validateToken = false)
	@RequestMapping(value = "transitionViewAttendanceMaintenanceList" , method = RequestMethod.POST)
	public ModelAndView transitionViewAttendanceMaintenanceList(ViewAttendanceMaintenance entity , PageBean paramPageBean) {
		ModelAndView mav = new ModelAndView(PropertiesReadUtil.getStringProperty("ViewTransitionAttendanceMaintenanceListView"));
		try {
			Map<String , String []> unLockMap = getParameterMap(request);
			String createTime_end = getParameter("createTime_end");
			SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
			if(createTime_end!=null && !createTime_end.equals("")){

			}else {
				Calendar lastDate = Calendar.getInstance();
				lastDate.add(Calendar.DAY_OF_YEAR, 0);
				String tomorrow = sdf.format(lastDate.getTime());
				unLockMap.put("createTime_end",new String[]{tomorrow});
			}
			DetachedCriteria dc = DetachedCriteria.forClass(ViewAttendanceMaintenance.class);
			HqlGenerateUtil.fillDetachedCriteria(dc , entity , unLockMap);
			String adminUserId = super.getLoginUser().getId();
			String ids = "";
			SysAdminUser sysAdminUser=this.sysAdminUserService.get(adminUserId);
			//获取当前登录者所拥有角色
			List<String> roleList = super.getLoginAdminRoles();

            if(roleList.contains(RoleConsts.TSS_COUNSELOR)||roleList.contains(RoleConsts.TSS_TEACHER)){
                dc.add(Restrictions.sqlRestriction(" adminUserId in ('" + adminUserId + "') "));
            } else{
                List<String> idsList = super.getUserIdByRoleAndSchololId(sysAdminUser.getSchoolId());
                ids = StringUtils.join(idsList, "','");
                dc.add(Restrictions.sqlRestriction(" adminUserId in ('" + ids + "') "));
            }
			dc.add(Restrictions.eq("type",2));
			dc.addOrder(Order.desc("createTime"));
			PageBean pageBean = tssMemberCheckAttendanceService.pageList(dc , paramPageBean.getPageNo() , paramPageBean.getPageSize());
			mav.addObject(super.SEARCH_MAP , unLockMap);
			mav.addObject(super.PAGE_BEAN , pageBean);
			mav.addObject("searchFormId" , SEARCH_FORM);
			mav.addObject("actionUrl" , request.getRequestURI());
			mav.addObject("pagingBar" , pageBean.getPagingBar(SEARCH_FORM));
			mav.addObject("serializeNum" , SequenceUtil.getUpperCaseUUID());
			String requestURI = StringUtils.substringBeforeLast(request.getRequestURI() , "/");
			mav.addObject("editUrl" , requestURI + "/toEditView.do");
			mav.addObject("deleteUrl" , requestURI + "/delete.do");
			mav.addObject("batchDeleteUrl" , requestURI + "/batchDelete.do");
		} catch (Exception e) {
			throw e;
		}
		return mav;
	}
	/**
	 * 转到编辑视图
	 *
	 * @param entity
	 *            转到会员考勤退课视图
	 * @param
	 * @return
	 */
	@RequestLog(content = "转到会员考勤退课视图" , save = true)
	@Token(validateToken = false)
	@RequestMapping(value = "ViewAttendanceMaintenanceListEditView" , method = RequestMethod.GET)
	public ModelAndView ViewAttendanceMaintenanceListEditView(ViewMemberAttendClassRecord entity , PageBean paramPageBean
			,@RequestParam(required = true) String attendanceId){
		ModelAndView mav = new ModelAndView(PropertiesReadUtil.getStringProperty("ViewAttendanceMaintenanceListEditView"));
		try {

			Map<String,Object> map = tssMemberCheckAttendanceService.findAttendanceMaintenance(attendanceId);

			mav.addObject("serializeNum" , SequenceUtil.getUpperCaseUUID());
			mav.addObject("Data" , map);
			mav.addObject("attendanceId" , attendanceId);
			String requestURI = StringUtils.substringBeforeLast(request.getRequestURI() , "/");
			mav.addObject("saveUrl" , requestURI + "/attendanceMaintenance.do");
		} catch (Exception e) {
			throw e;
		}
		return mav;
	}
	/**
	 * 考勤退课
	 *
	 * @param entity
	 *            被编辑对象
	 * @return
	 */
	@ResponseBody
	@RequestLog(content = "考勤退课" , save = true)
	@Token(validateToken = false)
	@RequestMapping(value = "attendanceMaintenance" , method = RequestMethod.POST)
	public String attendanceMaintenance(TssMemberCheckAttendance entity, @RequestParam(required = true) String attendanceId) {
		AjaxJsonResponse ajr = new AjaxJsonResponse();
		String adminUserId = super.getLoginUser().getId();
		try {
			//String classId=entity.getClassId();
			String[] str = request.getParameterValues("ids");
			ajr.setResMsg("修改成功!");
			List<String> ids = new ArrayList<String>();
			List<TssMemberCheckAttendance> history = new ArrayList<TssMemberCheckAttendance>();
			ids.add(0,attendanceId);
			if(ids.size() > 0){
				history = tssMemberCheckAttendanceService.findHistory(ids);
				//更新考勤退课状态
				int j = this.tssMemberCheckAttendanceService.updateCheckAttendance(adminUserId,entity.getRemark(),entity.getAttendenceStatus(),attendanceId);

				//查询课时包相关信息
				Map<String, Object> attendanceMaintenancePackageMap = tssMemberCheckAttendanceService.findPackageInformation(attendanceId);
				Integer isComplimentaryMeals = Integer.parseInt(attendanceMaintenancePackageMap.get("isComplimentaryMeals").toString());
				Integer formalClass = Integer.parseInt(attendanceMaintenancePackageMap.get("formalClass").toString());
				//考勤退课退还课时
				boolean a = false;
				boolean b = false;
				boolean c = false;
				boolean d = false;
				if(history.get(0).getType() == 1){
					a = tssMemberPackageService.attendanceMaintenanceForParentingPackage(attendanceMaintenancePackageMap);

					//退课时权责
					TssRightStatistic tssRightStatistic = tssRightStatisticService.findByAttendanceIdAndtype(attendanceMaintenancePackageMap.get("id"),1);

					c = tssRightStatisticService.saveOrUpdateTssRightStatisticService(tssRightStatistic,attendanceMaintenancePackageMap,adminUserId,1,2,0,null);
				}else{
					a = tssMemberPackageService.attendanceMaintenanceForTransitionPackage(attendanceMaintenancePackageMap);

					//退课时权责
					TssRightStatistic tssRightStatistic = tssRightStatisticService.findByAttendanceIdAndtype(attendanceMaintenancePackageMap.get("id"),1);

					c = tssRightStatisticService.saveOrUpdateTssRightStatisticService(tssRightStatistic,attendanceMaintenancePackageMap,adminUserId,2,5,0,null);

					if(isComplimentaryMeals == 0){
						//加餐点费
						TssMealFee tssMealFee = tssMealFeeService.findByStuId(attendanceMaintenancePackageMap.get("memberId").toString());

						b = tssMealFeeService.plusDayMealFee(tssMealFee);

						//餐点费权责
						TssRightStatistic tssRightStatisticDayMealFee = tssRightStatisticService.findByAttendanceIdAndtype(attendanceMaintenancePackageMap.get("id"),3);

						attendanceMaintenancePackageMap.put("dayMealFee",tssMealFee.getDayMealFee());
						d = tssRightStatisticService.saveOrUpdateTssRightStatisticService(tssRightStatisticDayMealFee,attendanceMaintenancePackageMap,adminUserId,3,15,0,tssMealFee.getDayMealFee());
					}
					if(isComplimentaryMeals == 1){
						if(formalClass > 0){
							//加餐点费
							TssMealFee tssMealFee = tssMealFeeService.findByStuId(attendanceMaintenancePackageMap.get("memberId").toString());

							b = tssMealFeeService.plusDayMealFee(tssMealFee);

							//餐点费权责
							TssRightStatistic tssRightStatisticDayMealFee = tssRightStatisticService.findByAttendanceIdAndtype(attendanceMaintenancePackageMap.get("id"),3);

							attendanceMaintenancePackageMap.put("dayMealFee",tssMealFee.getDayMealFee());
							d = tssRightStatisticService.saveOrUpdateTssRightStatisticService(tssRightStatisticDayMealFee,attendanceMaintenancePackageMap,adminUserId,3,15,0,tssMealFee.getDayMealFee());
						}
					}


				}
				if(!a){
					ajr.setResMsg("修改失败!");
				}
				//更新班级考勤状态
				/*if(entity.getCreateTime() == null){
					entity.setCreateTime(new Date());
				}
				tssMemberCheckAttendanceService.updateClassAttendance(entity.getClassId(),entity.getCreateTime());*/
				//添加操作记录
				tssMemberCheckAttendanceHistoryService.batchSave(history);
			} else {
				ajr.setResCode(301);
				ajr.setResMsg("修改失败!");
			}

		} catch (Exception e) {
			e.printStackTrace();
			ajr.setResCode(500);
			ajr.setResMsg("修改失败(" + e.getMessage() + ")!");
		}
		return ajr.jsonResponse(false);
	}
	/**
	 * 批量考勤
	 *
	 * @param entity
	 *            被编辑对象
	 * @return
	 */
	@ResponseBody
	@RequestLog(content = "批量考勤" , save = true)
	@Token(validateToken = false)
	@RequestMapping(value = "batchAttendance" , method = RequestMethod.GET)
	public String batchAttendance(TssMemberCheckAttendance entity, @RequestParam("ids[]")List<String> ids,@RequestParam(required = true) String lessonsId) {
		AjaxJsonResponse ajr = new AjaxJsonResponse();
		String adminUserId = super.getLoginUser().getId();
		try {
			//String classId=entity.getClassId();
			String[] str = request.getParameterValues("ids");
			ajr.setResMsg("修改成功!");
			List<TssMemberCheckAttendance> history = new ArrayList<TssMemberCheckAttendance>();
			List<TssMemberPackage> memberPackagehistorys = new ArrayList<TssMemberPackage>();
			if(ids.size() > 0){
				history = tssMemberCheckAttendanceService.findHistory(ids);
				memberPackagehistorys = tssMemberPackageService.findHistory(ids);

				int i = tssMemberCheckAttendanceService.batchAttendance(ids,adminUserId);

				//查询课时包相关信息
				List<Map<String, Object>> packageInformation = tssMemberCheckAttendanceService.findPackageInformationList(ids);
				//批量扣除课时
				boolean c = false;
				boolean d = false;
				if(entity.getType() == 1){
					boolean a = tssMemberPackageService.batchDeductiveHours(packageInformation);
					//批量产生课时权责
					for (Map<String,Object> map : packageInformation) {
						map.get("id");
						TssRightStatistic tssRightStatistic = tssRightStatisticService.findByAttendanceIdAndtype(map.get("id"),1);

						c = tssRightStatisticService.saveOrUpdateTssRightStatisticService(tssRightStatistic,map,adminUserId,1,1,0,null);
						if(!c){
							ajr.setResMsg("修改失败!");
						}
					}
				}else {
					boolean a = tssMemberPackageService.batcheductiveAttendanceDay(packageInformation);

					//扣餐点费  赠送天数不计数的不扣除餐点费
					boolean b = tssMealFeeService.batchDeductionDayMealFee(packageInformation);


					//批量产生课时权责
					for (Map<String,Object> map : packageInformation) {
						//课时权责
						TssRightStatistic tssRightStatistic = tssRightStatisticService.findByAttendanceIdAndtype(map.get("id"),2);

						c = tssRightStatisticService.saveOrUpdateTssRightStatisticService(tssRightStatistic,map,adminUserId,2,4,0,null);

						Integer isComplimentaryMeals = Integer.parseInt(map.get("isComplimentaryMeals").toString());
						Integer formalClass = Integer.parseInt(map.get("formalClass").toString());
						if(isComplimentaryMeals == 0){
							//餐点费权责
							TssRightStatistic tssRightStatisticDayMealFee = tssRightStatisticService.findByAttendanceIdAndtype(map.get("id"),3);

							TssMealFee tssMealFee = tssMealFeeService.findByStuId(map.get("memberId").toString());
							map.put("dayMealFee",tssMealFee.getDayMealFee());
							d = tssRightStatisticService.saveOrUpdateTssRightStatisticService(tssRightStatisticDayMealFee,map,adminUserId,3,7,0,tssMealFee.getDayMealFee());
						}
						if(isComplimentaryMeals == 1){
							if(formalClass > 0){
								//餐点费权责
								TssRightStatistic tssRightStatisticDayMealFee = tssRightStatisticService.findByAttendanceIdAndtype(map.get("id"),3);

								TssMealFee tssMealFee = tssMealFeeService.findByStuId(map.get("memberId").toString());
								map.put("dayMealFee",tssMealFee.getDayMealFee());
								d = tssRightStatisticService.saveOrUpdateTssRightStatisticService(tssRightStatisticDayMealFee,map,adminUserId,3,7,0,tssMealFee.getDayMealFee());
							}
						}
					}
				}
				//更新班级考勤状态
				/*if(entity.getCreateTime() == null){
					entity.setCreateTime(new Date());
				}*/
				tssMemberCheckAttendanceService.updateClassAttendance(lessonsId);
				//添加操作记录
				if(history.size()>0){
					tssMemberCheckAttendanceHistoryService.batchSave(history);
				}
				//添加课时包记录
				if(memberPackagehistorys.size()>0){
					tssMemberPackageHistoryService.batchSave(memberPackagehistorys);
				}
			} else {
				ajr.setResCode(301);
				ajr.setResMsg("修改失败!");
			}
		} catch (Exception e) {
			e.printStackTrace();
			ajr.setResCode(500);
			ajr.setResMsg("修改失败(" + e.getMessage() + ")!");
		}
		return ajr.jsonResponse(false);
	}
	/**
	 * 班级批量考勤
	 *
	 * @param entity
	 *            被编辑对象
	 * @return
	 */
	@ResponseBody
	@RequestLog(content = "班级批量考勤" , save = true)
	@Token(validateToken = false)
	@RequestMapping(value = "classBatchAttendance" , method = RequestMethod.GET)
	public String classBatchAttendance(TssMemberCheckAttendance entity, @RequestParam("ids[]")List<String> lessonsIds) {
		AjaxJsonResponse ajr = new AjaxJsonResponse();
		String adminUserId = super.getLoginUser().getId();
		try {
			//String classId=entity.getClassId();
			//String[] str = request.getParameterValues("ids");
			//查询选中班级中未考勤的人员
			List<String> ids = new ArrayList<String>();
			//查询课时包历史
			List<TssMemberPackage> memberPackagehistorys = new ArrayList<TssMemberPackage>();
			if(lessonsIds.size() > 0) {
				ids = tssMemberCheckAttendanceService.notAttendanceIds(lessonsIds);
				memberPackagehistorys = tssMemberPackageService.findHistory(ids);
			}
			ajr.setResMsg("修改成功!");
			List<TssMemberCheckAttendance> history = new ArrayList<TssMemberCheckAttendance>();
			if(ids.size() > 0){
				history = tssMemberCheckAttendanceService.findHistory(ids);
				int i = tssMemberCheckAttendanceService.batchAttendance(ids,adminUserId);

			//查询课时包相关信息
			List<Map<String, Object>> packageInformation = tssMemberCheckAttendanceService.findPackageInformationList(ids);
			boolean c = false;
			boolean d = false;
			//批量扣除课时
			if(history.get(0).getType() == 1){
				boolean a = tssMemberPackageService.batchDeductiveHours(packageInformation);

				//批量产生课时权责
				for (Map<String,Object> map : packageInformation) {
					map.get("id");
					TssRightStatistic tssRightStatistic = tssRightStatisticService.findByAttendanceIdAndtype(map.get("id"),1);

					c = tssRightStatisticService.saveOrUpdateTssRightStatisticService(tssRightStatistic,map,adminUserId,1,1,0,null);
					if(!c){
						ajr.setResMsg("修改失败!");
					}
				}
			}else {
				boolean a = tssMemberPackageService.batcheductiveAttendanceDay(packageInformation);
				//扣餐点费
				boolean b = tssMealFeeService.batchDeductionDayMealFee(packageInformation);

				//批量产生课时权责
				for (Map<String,Object> map : packageInformation) {
					//课时权责
					TssRightStatistic tssRightStatistic = tssRightStatisticService.findByAttendanceIdAndtype(map.get("id"),2);

					c = tssRightStatisticService.saveOrUpdateTssRightStatisticService(tssRightStatistic,map,adminUserId,2,4,0,null);

					Integer isComplimentaryMeals = Integer.parseInt(map.get("isComplimentaryMeals").toString());
					Integer formalClass = Integer.parseInt(map.get("formalClass").toString());
					if(isComplimentaryMeals == 0){
						//餐点费权责
						TssRightStatistic tssRightStatisticDayMealFee = tssRightStatisticService.findByAttendanceIdAndtype(map.get("id"),3);

						TssMealFee tssMealFee = tssMealFeeService.findByStuId(map.get("memberId").toString());
						map.put("dayMealFee",tssMealFee.getDayMealFee());
						d = tssRightStatisticService.saveOrUpdateTssRightStatisticService(tssRightStatisticDayMealFee,map,adminUserId,3,7,0,tssMealFee.getDayMealFee());
					}
					if(isComplimentaryMeals == 1){
						if(formalClass > 0){
							//餐点费权责
							TssRightStatistic tssRightStatisticDayMealFee = tssRightStatisticService.findByAttendanceIdAndtype(map.get("id"),3);

							TssMealFee tssMealFee = tssMealFeeService.findByStuId(map.get("memberId").toString());
							map.put("dayMealFee",tssMealFee.getDayMealFee());
							d = tssRightStatisticService.saveOrUpdateTssRightStatisticService(tssRightStatisticDayMealFee,map,adminUserId,3,7,0,tssMealFee.getDayMealFee());
						}
					}
				}
			}
			//更新班级考勤状态
			if(lessonsIds.size() > 0) {
				for (int j = 0; j < lessonsIds.size(); j++) {
					tssMemberCheckAttendanceService.updateClassAttendance(lessonsIds.get(j));
				}
			}
			//tssMemberCheckAttendanceHistoryService.batchSave(history);
			//添加操作记录
			if(history.size()>0){
				tssMemberCheckAttendanceHistoryService.batchSave(history);
			}
			//添加课时包记录
			if(memberPackagehistorys.size()>0){
				tssMemberPackageHistoryService.batchSave(memberPackagehistorys);
			}

			} else {
				ajr.setResCode(301);
				ajr.setResMsg("修改失败!");
			}
		} catch (Exception e) {
			e.printStackTrace();
			ajr.setResCode(500);
			ajr.setResMsg("修改失败(" + e.getMessage() + ")!");
		}
		return ajr.jsonResponse(false);
	}
	/**
	 * 考勤
	 *
	 * @param entity
	 *            被编辑对象
	 * @return
	 */
	@ResponseBody
	@RequestLog(content = "考勤" , save = true)
	@Token(validateToken = false)
	@RequestMapping(value = "attendance" , method = RequestMethod.GET)
	public String attendance(TssMemberCheckAttendance entity,@RequestParam(required = true) String lessonsId) {
		AjaxJsonResponse ajr = new AjaxJsonResponse();
		String adminUserId = super.getLoginUser().getId();
		try {
			String id=entity.getId();
			ajr.setResMsg("修改成功!");
			List<String> ids = new ArrayList<String>();
			List<TssMemberCheckAttendance> history = new ArrayList<TssMemberCheckAttendance>();
			List<TssMemberPackage> memberPackagehistorys = new ArrayList<TssMemberPackage>();
			if(StringUtils.isNotBlank(id)){
				ids.add(0,id);
				history = tssMemberCheckAttendanceService.findHistory(ids);
				memberPackagehistorys = tssMemberPackageService.findHistory(ids);
				int i = this.tssMemberCheckAttendanceService.attendance(entity.getAttendenceStatus(),adminUserId,entity.getId());
				//查询课时相关
				Map<String, Object> packageInformation = tssMemberCheckAttendanceService.findPackageInformation(id);
				Integer isComplimentaryMeals = Integer.parseInt(packageInformation.get("isComplimentaryMeals").toString());
				Integer formalClass = Integer.parseInt(packageInformation.get("formalClass").toString());
				boolean a = false;
				boolean b = false;
				boolean c = false;
				boolean d = false;
				TssMealFee tssMealFee = tssMealFeeService.findByStuId(packageInformation.get("memberId").toString());
				//修改课时 扣餐点费
				//考勤
				if(entity.getAttendenceStatus() == 1){
					//历史考勤是未出勤
					if(history.get(0).getAttendenceStatus() == 0){
						if(entity.getType() == 1){
							a = tssMemberPackageService.deductiveHours(packageInformation);

							//课时权责
							TssRightStatistic tssRightStatistic = tssRightStatisticService.findByAttendanceIdAndtype(packageInformation.get("id"),1);

							c = tssRightStatisticService.saveOrUpdateTssRightStatisticService(tssRightStatistic,packageInformation,adminUserId,1,1,0,null);
						}else {
							a = tssMemberPackageService.deductiveAttendanceDay(packageInformation);
							//课时权责
							TssRightStatistic tssRightStatistic = tssRightStatisticService.findByAttendanceIdAndtype(packageInformation.get("id"),2);

							c = tssRightStatisticService.saveOrUpdateTssRightStatisticService(tssRightStatistic,packageInformation,adminUserId,2,4,0,null);

							//是否赠送餐点费 0不赠送 1赠送'   赠送餐点费则不扣除餐点费不产生权责
							if(isComplimentaryMeals == 0){
								//扣餐点费
								b = tssMealFeeService.deductionDayMealFee(tssMealFee);

								//餐点费权责
								TssRightStatistic tssRightStatisticDayMealFee = tssRightStatisticService.findByAttendanceIdAndtype(packageInformation.get("id"),3);

								packageInformation.put("dayMealFee",tssMealFee.getDayMealFee());
								d = tssRightStatisticService.saveOrUpdateTssRightStatisticService(tssRightStatisticDayMealFee,packageInformation,adminUserId,3,7,0,tssMealFee.getDayMealFee());
							}if(isComplimentaryMeals == 1){
								if(formalClass > 0){
									//扣餐点费
									b = tssMealFeeService.deductionDayMealFee(tssMealFee);

									//餐点费权责
									TssRightStatistic tssRightStatisticDayMealFee = tssRightStatisticService.findByAttendanceIdAndtype(packageInformation.get("id"),3);

									packageInformation.put("dayMealFee",tssMealFee.getDayMealFee());
									d = tssRightStatisticService.saveOrUpdateTssRightStatisticService(tssRightStatisticDayMealFee,packageInformation,adminUserId,3,7,0,tssMealFee.getDayMealFee());
								}
							}

							//查询餐点费是否  负费  进行提示
							TssMealFee newTssMealFee = tssMealFeeService.findByStuId(packageInformation.get("memberId").toString());
							if(newTssMealFee != null){
								if(newTssMealFee.getSurplusAmout() < 0.00){
									ajr.setResMsg("！餐点费余额不足，餐点费："+newTssMealFee.getSurplusAmout()+"");
								}
							}

						}
					}
					//历史是缺勤
					if(history.get(0).getAttendenceStatus() == 2){
						if(entity.getType() == 1){
							a = tssMemberPackageService.deductiveHours(packageInformation);

							//课时权责
							TssRightStatistic tssRightStatistic = tssRightStatisticService.findByAttendanceIdAndtype(packageInformation.get("id"),1);

							c = tssRightStatisticService.saveOrUpdateTssRightStatisticService(tssRightStatistic,packageInformation,adminUserId,1,1,0,null);
						}else {
							a = tssMemberPackageService.deductiveAttendanceDay(packageInformation);

							//课时权责
							TssRightStatistic tssRightStatistic = tssRightStatisticService.findByAttendanceIdAndtype(packageInformation.get("id"),2);

							c = tssRightStatisticService.saveOrUpdateTssRightStatisticService(tssRightStatistic,packageInformation,adminUserId,2,4,0,null);

							//是否赠送餐点费 0不赠送 1赠送'   赠送餐点费则不扣除餐点费不产生权责
							if(isComplimentaryMeals == 0){
								//扣餐点费
								b = tssMealFeeService.deductionDayMealFee(tssMealFee);

								//餐点费权责
								TssRightStatistic tssRightStatisticDayMealFee = tssRightStatisticService.findByAttendanceIdAndtype(packageInformation.get("id"),3);

								packageInformation.put("dayMealFee",tssMealFee.getDayMealFee());
								d = tssRightStatisticService.saveOrUpdateTssRightStatisticService(tssRightStatisticDayMealFee,packageInformation,adminUserId,3,7,0,tssMealFee.getDayMealFee());
							}if(isComplimentaryMeals == 1){
								if(formalClass > 0){
									//扣餐点费
									b = tssMealFeeService.deductionDayMealFee(tssMealFee);

									//餐点费权责
									TssRightStatistic tssRightStatisticDayMealFee = tssRightStatisticService.findByAttendanceIdAndtype(packageInformation.get("id"),3);

									packageInformation.put("dayMealFee",tssMealFee.getDayMealFee());
									d = tssRightStatisticService.saveOrUpdateTssRightStatisticService(tssRightStatisticDayMealFee,packageInformation,adminUserId,3,7,0,tssMealFee.getDayMealFee());
								}
							}
						}
					}
					//历史是缺勤可补课
					if(history.get(0).getAttendenceStatus() == 3){
						if(entity.getType() == 1){
							a = tssMemberPackageService.reductionMakeUpClassDeductiveHours(packageInformation);

							//课时权责
							TssRightStatistic tssRightStatistic = tssRightStatisticService.findByAttendanceIdAndtype(packageInformation.get("id"),1);

							c = tssRightStatisticService.saveOrUpdateTssRightStatisticService(tssRightStatistic,packageInformation,adminUserId,1,1,0,null);
						}
					}
				}
				//缺勤
				if(entity.getAttendenceStatus() == 2){
					//历史是未考勤
					if(history.get(0).getAttendenceStatus() == 0){
						//维护缺勤天数
						tssMemberPackageService.absenceRecord(packageInformation);
					}
					//历史是出勤
					if(history.get(0).getAttendenceStatus() == 1){
						if(entity.getType() == 1){
							a = tssMemberPackageService.absenteeismDeductiveHours(packageInformation);

							//课时权责
							TssRightStatistic tssRightStatistic = tssRightStatisticService.findByAttendanceIdAndtype(packageInformation.get("id"),1);

							c = tssRightStatisticService.saveOrUpdateTssRightStatisticService(tssRightStatistic,packageInformation,adminUserId,1,1,1,null);
						}else {
							a = tssMemberPackageService.absenteeismDeductiveAttendanceDay(packageInformation);

							//课时权责
							TssRightStatistic tssRightStatistic = tssRightStatisticService.findByAttendanceIdAndtype(packageInformation.get("id"),2);

							c = tssRightStatisticService.saveOrUpdateTssRightStatisticService(tssRightStatistic,packageInformation,adminUserId,1,4,1,null);

							//是否赠送餐点费 0不赠送 1赠送'   赠送餐点费则不扣除餐点费不产生权责
							if(isComplimentaryMeals == 0){
								//加餐点费
								b = tssMealFeeService.plusDayMealFee(tssMealFee);
								//餐点费权责
								TssRightStatistic tssRightStatisticDayMealFee = tssRightStatisticService.findByAttendanceIdAndtype(packageInformation.get("id"),3);

								d = tssRightStatisticService.saveOrUpdateTssRightStatisticService(tssRightStatisticDayMealFee,packageInformation,adminUserId,3,7,1,tssMealFee.getDayMealFee());
							}
							if(isComplimentaryMeals == 1){
								if(formalClass > 0){
									//加餐点费
									b = tssMealFeeService.plusDayMealFee(tssMealFee);
									//餐点费权责
									TssRightStatistic tssRightStatisticDayMealFee = tssRightStatisticService.findByAttendanceIdAndtype(packageInformation.get("id"),3);

									d = tssRightStatisticService.saveOrUpdateTssRightStatisticService(tssRightStatisticDayMealFee,packageInformation,adminUserId,3,7,1,tssMealFee.getDayMealFee());
								}
							}

						}
					}
					//历史是缺勤可补课
					if(history.get(0).getAttendenceStatus() == 3){
						if(entity.getType() == 1){
							a = tssMemberPackageService.reductionAbsenteeismDeductiveHours(packageInformation);

							//课时权责
							TssRightStatistic tssRightStatistic = tssRightStatisticService.findByAttendanceIdAndtype(packageInformation.get("id"),1);

							c = tssRightStatisticService.saveOrUpdateTssRightStatisticService(tssRightStatistic,packageInformation,adminUserId,1,1,1,null);
						}
					}
				}
				//缺勤可补课
				if(entity.getAttendenceStatus() == 3){
					//历史是未考勤
					if(history.get(0).getAttendenceStatus() == 0){
						if(entity.getType() == 1){
							a = tssMemberPackageService.makeUpClassDeductiveHours(packageInformation);

							//课时权责
							TssRightStatistic tssRightStatistic = tssRightStatisticService.findByAttendanceIdAndtype(packageInformation.get("id"),1);

							c = tssRightStatisticService.saveOrUpdateTssRightStatisticService(tssRightStatistic,packageInformation,adminUserId,1,1,0,null);
						}
					}
					//历史是出勤
					if(history.get(0).getAttendenceStatus() == 1){
						if(entity.getType() == 1){
							a = tssMemberPackageService.addMakeUpClassDeductiveHours(packageInformation);

							//课时权责
							TssRightStatistic tssRightStatistic = tssRightStatisticService.findByAttendanceIdAndtype(packageInformation.get("id"),1);

							c = tssRightStatisticService.saveOrUpdateTssRightStatisticService(tssRightStatistic,packageInformation,adminUserId,1,1,0,null);
						}
					}
					////历史是缺勤
					if(history.get(0).getAttendenceStatus() == 2){
						if(entity.getType() == 1){
							a = tssMemberPackageService.makeUpClassDeductiveHours(packageInformation);

							//课时权责
							TssRightStatistic tssRightStatistic = tssRightStatisticService.findByAttendanceIdAndtype(packageInformation.get("id"),1);

							c = tssRightStatisticService.saveOrUpdateTssRightStatisticService(tssRightStatistic,packageInformation,adminUserId,1,1,0,null);
						}
					}

				}
				if(!a){
					ajr.setResMsg("修改失败!");
				}
				if(!c){
					ajr.setResMsg("修改失败!");
				}
				if(entity.getType() == 2){
					if(!b){
						ajr.setResMsg("修改失败!");
					}
					if(!d){
						ajr.setResMsg("修改失败!");
					}
				}
			} else {
				ajr.setResCode(301);
				ajr.setResMsg("修改失败!");
			}
			if(entity.getCreateTime() == null){
				SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd 00:00:00");
				entity.setCreateTime(formatter.parse(formatter.format(new Date())));
			}
			//更新班级考勤状态
			tssMemberCheckAttendanceService.updateClassAttendance(lessonsId);
			//添加操作记录
			if(history.size()>0){
				tssMemberCheckAttendanceHistoryService.batchSave(history);
			}
			//添加课时包记录
			if(memberPackagehistorys.size()>0){
				tssMemberPackageHistoryService.batchSave(memberPackagehistorys);
			}
		} catch (Exception e) {
			e.printStackTrace();
			ajr.setResCode(500);
			ajr.setResMsg("修改失败(" + e.getMessage() + ")!");
		}
		return ajr.jsonResponse(false);
	}
	/**
	 * 备注
	 *
	 * @param entity
	 *            被编辑对象
	 * @return
	 */
	@ResponseBody
	@RequestLog(content = "备注" , save = true)
	@Token(validateToken = false)
	@RequestMapping(value = "Remarks" , method = RequestMethod.GET)
	public String Remarks(TssMemberCheckAttendance entity) {
		AjaxJsonResponse ajr = new AjaxJsonResponse();
		String adminUserId = super.getLoginUser().getId();
		try {
			String id=entity.getId();
			ajr.setResMsg("修改成功!");
			List<String> ids = new ArrayList<String>();
			List<TssMemberCheckAttendance> history = new ArrayList<TssMemberCheckAttendance>();
			if(StringUtils.isNotBlank(id)){
				ids.add(0,id);
				history = tssMemberCheckAttendanceService.findHistory(ids);
				/*String sql = "update tss_member_check_attendance set `remark`='"+entity.getRemark()+"', adminUserId ='"+adminUserId+"'where id = '"+entity.getId()+"'";
				System.out.println(sql);
				int i = this.tssMemberCheckAttendanceService.updateBySql(sql);*/
				int i = this.tssMemberCheckAttendanceService.Remarks(entity.getRemark(),adminUserId,entity.getId());
				//添加操作记录
				tssMemberCheckAttendanceHistoryService.batchSave(history);
			} else {
				ajr.setResCode(301);
				ajr.setResMsg("修改失败!");
			}
		} catch (Exception e) {
			e.printStackTrace();
			ajr.setResCode(500);
			ajr.setResMsg("修改失败(" + e.getMessage() + ")!");
		}
		return ajr.jsonResponse(false);
	}
    /**
     * 备注
     *
     * @param
     * @return
     */
    @RequestLog(content = "备注" , save = true)
    @Token(validateToken = false)
    @RequestMapping(value = "test" , method = RequestMethod.GET)
    public void test() {
        try {
            System.out.println("-----------------------测试"+new Date()+"------------------------");
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
	/**
	 * 转到编辑视图
	 * 
	 * @param entity
	 *            接收编辑参数的临时对象
	 * @param serializeNum
	 *            请求标识
	 * @return
	 */
	@RequestLog(content = "转到会员考勤记录表视图" , save = true)
	@Token(validateToken = false)
	@RequestMapping(value = "toEditView" , method = RequestMethod.GET)
	public ModelAndView toEditView(TssMemberCheckAttendance entity , @RequestParam(required = true) String serializeNum) {
		ModelAndView mav = new ModelAndView(PropertiesReadUtil.getStringProperty("tssMemberCheckAttendanceEditView"));
		if(entity != null && entity.getId() != null) {
			TssMemberCheckAttendance editEntity = this.tssMemberCheckAttendanceService.get(entity.getId());
			mav.addObject("editEntity" , editEntity);
			mav.addObject("editModel" , "edit");
		}else{
			mav.addObject("editModel" , "add");
		}
		mav.addObject("serializeNum" , serializeNum);
		mav.addObject("saveUrl" , StringUtils.substringBeforeLast(request.getRequestURI() , "/") + "/saveOrUpdate.do");
		return mav;
	}
	
	/**
	 * 保存编辑的对象
	 * 
	 * @param entity
	 *            被编辑对象
	 * @return
	 */
	@ResponseBody
	@RequestLog(content = "保存会员考勤记录表对象" , save = true)
	@Token(validateToken = false)
	@RequestMapping(value = "saveOrUpdate" , method = RequestMethod.POST)
	public String saveOrUpdate(TssMemberCheckAttendance entity,@RequestParam(required = true) String flag) {
		AjaxJsonResponse ajr = new AjaxJsonResponse();
		try {
			String adminUserId = super.getLoginUser().getId();
			String id=entity.getId();
			ajr.setResMsg("保存成功!");
			if(StringUtils.isNotBlank(id)){
				TssMemberCheckAttendance destEntity = this.tssMemberCheckAttendanceService.get(id);
				//考勤
				BeanUtils.copyProperties(entity , destEntity ,"createTime");
				this.tssMemberCheckAttendanceService.updateEntitiy(destEntity);

			} else {
				entity.setCreateTime(new java.sql.Timestamp(System.currentTimeMillis()));
				Serializable newId = this.tssMemberCheckAttendanceService.save(entity);
				if(newId == null) {
					ajr.setResCode(301);
					ajr.setResMsg("保存失败!");
				}
			}
		} catch (Exception e) {
			e.printStackTrace();
			ajr.setResCode(500);
			ajr.setResMsg("保存失败(" + e.getMessage() + ")!");
		}
		return ajr.jsonResponse(false);
	}
	
	/**
	 * 删除指定id的对象
	 * 
	 * @param id
	 *            需要被删除的对象id
	 * @return
	 */
	@ResponseBody
	@RequestLog(content = "删除属性" , save = true)
	@Token(validateToken = false)
	@RequestMapping(value = "delete" , method = RequestMethod.GET)
	public String delete(@StringValidator(nullable = false) String id) {
		AjaxJsonResponse ajr = new AjaxJsonResponse();
		try {
			ajr.setResMsg("删除成功!");
			if(StringUtils.isNotBlank(id)) {
				this.tssMemberCheckAttendanceService.deleteEntityById(id);
			} else {
				ajr.setResMsg("删除失败!");
				ajr.setResCode(302);
			}
		} catch (Exception e) {
			e.printStackTrace();
			ajr.setResCode(500);
			ajr.setResMsg("删除失败(" + e.getMessage() + ")!");
		}
		return ajr.jsonResponse(false);
	}
	
	/**
	 * 批量删除指定id的对象
	 * 
	 * @param ids
	 *            需要被删除的对象id组成的字符串
	 * @return
	 */
	@ResponseBody
	@RequestLog(content = "删除属性" , save = true)
	@Token(validateToken = false)
	@RequestMapping(value = "batchDelete" , method = RequestMethod.GET)
	public String batchDelete(@RegexpValidator(nullable = false , regexp = "^(\\w+,?)+\\w+$") String ids) {
		AjaxJsonResponse ajr = new AjaxJsonResponse();
		try {
			ajr.setResMsg("删除成功!");
			if(StringUtils.isNotBlank(ids)) {
				boolean batchDelete = this.tssMemberCheckAttendanceService.batchDelete(ids.split(","));
				if(!batchDelete) {
					ajr.setResMsg("删除失败!");
					ajr.setResCode(302);
				}
			}
		} catch (Exception e) {
			e.printStackTrace();
			ajr.setResCode(500);
			ajr.setResMsg("删除失败(" + e.getMessage() + ")!");
		}
		return ajr.jsonResponse(false);
	}
}
